package com.ebelter.bodyfatscale.util;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.TextView;

import com.ebelter.bodyfatscale.ApplicationHelper;
import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.common.Constants;
import com.ebelter.btlibrary.btble.impl.scale.model.ScaleMeasureResult;
import com.ebelter.btlibrary.util.ULog;

import java.util.Date;
import java.util.Locale;

/**
 * 体脂的通用基本工具
 */

public class ScaleBaseUtils {



private static final String TAG="ScaleBaseUtils";
    public static ScaleMeasureResult checkScaleMeasureResult18old(ScaleMeasureResult result){
     if (result==null)return null;
        ULog.i(TAG,result.toString());
     if (result.getAge()<18){
         float tempValue=0;
         result.setVisceralFat(tempValue);
         result.setBodyAge(tempValue);
         result.setBodyScore(tempValue);
         result.setSkeletalMuscle(tempValue);
         result.setBmr(tempValue);
         result.setMuscleVolume(tempValue);
         result.setBoneVolume(tempValue);
         result.setProtein(tempValue);
         result.setFat(tempValue);
         result.setWaterRate(tempValue);
     }
        result.setVisceralFat(revisedData(result.getVisceralFat()));
        result.setBodyAge(revisedData(result.getBodyAge()));
        result.setBodyScore(revisedData(result.getBodyScore()));
        result.setSkeletalMuscle(revisedData(result.getSkeletalMuscle()));
        result.setBmr(revisedData(result.getBmr()));
        result.setMuscleVolume(revisedData(result.getMuscleVolume()));
        result.setBoneVolume(revisedData(result.getBoneVolume()));
        result.setProtein(revisedData(result.getProtein()));
        result.setFat(revisedData(result.getFat()));
        result.setWaterRate(revisedData(result.getWaterRate()));

     return result;
    }

    private static float revisedData(float value){
        if (value<0){
            value=0;
        }
        return value;
    }

    /**
     *
     * @param timeStr 1970-01-01 08:00:00 时间格式永远是这样的
     * @param fatmat 1年月日 2时分
     */
    public static String timeFatmat(String timeStr, int fatmat){
        if (TextUtils.isEmpty(timeStr)){
            return "";
        }
        if (timeStr.length()==10){
            //说明时间可能是1970-01-01
            timeStr=timeStr+" 00:00:00";
        }
        String result="";
        if (timeStr.length()!=19){
            ULog.i("ScaleBaseUtils",timeStr);
            return result;//说明传进来的时间格式是不正确的
        }
        //String string="1994-16-18 18:19:32";
            int year=NumUtils.string2Int(timeStr.substring(0,4));
            int month=NumUtils.string2Int(timeStr.substring(5,7));
            int day=NumUtils.string2Int(timeStr.substring(8,10));
            int h=NumUtils.string2Int(timeStr.substring(11,13));
            int m=NumUtils.string2Int(timeStr.substring(14,16));
            int s=NumUtils.string2Int(timeStr.substring(17,19));
       if (fatmat==1){
           result = SpecialText.formatDateStampString1(new Date(year-1900,month-1,day,h,m,s).getTime());
        }else if (fatmat==2){
           result=SpecialText.formatDateStampString2(new Date(year-1900,month-1,day,h,m,s).getTime());
        }else if (fatmat==3){
           result=SpecialText.formatDateStampString3(new Date(year-1900,month-1,day,h,m,s).getTime());
       }else if (fatmat==0){
           result = SpecialText.formatDateStampString0(new Date(year-1900,month-1,day,h,m,s).getTime());
       }
        ULog.i("ScaleBaseUtils","--fatmat="+fatmat+"----year="+year+"--month="+month+"--day="+day+"--h="+h+"--m="+m+"--s="+s+"\n"+result);

        return result;
    }

    /**
     * @param timeStr 1970-01-01 08:00:00 时间格式永远是这样的
     */
    public static long getTime(String timeStr){
        if (TextUtils.isEmpty(timeStr)){
            return 0;
        }
        if (timeStr.length()==10){
            //说明时间可能是1970-01-01
            timeStr=timeStr+" 00:00:00";
        }
        long time;
        if (timeStr.length()!=19){
            ULog.i("ScaleBaseUtils--getTime",timeStr);
            return 0;//说明传进来的时间格式是不正确的
        }

        int year=NumUtils.string2Int(timeStr.substring(0,4));
        int month=NumUtils.string2Int(timeStr.substring(5,7));
        int day=NumUtils.string2Int(timeStr.substring(8,10));
        int h=NumUtils.string2Int(timeStr.substring(11,13));
        int m=NumUtils.string2Int(timeStr.substring(14,16));
        int s=NumUtils.string2Int(timeStr.substring(17,19));

        time = new Date(year-1900,month-1,day,h,m,s).getTime();
        return time;
    }

    //
    public static void setTextViewSize1(Context context,TextView...textViews){
        if (context==null||textViews==null||textViews.length==0){
            return;
        }
        float size=10;
        Locale locale=context.getResources().getConfiguration().locale;
        String ct=locale.getCountry().toLowerCase();
        if (ct.contains("cn")){//中文
            size=11;
        }
        for (TextView textView:textViews){
            textView.setTextSize(size);
        }
    }




    /**根据资源获取字符串*/
    public static String getStr(int res){
        return ApplicationHelper.mApplicationContecx.getResources().getString(res);
    }

    //根据身高返回建议体重的最小值 height是cm 返回的是kg
    public static float getIdeaWeightMin(int height){
        return (float) (height * height * 0.0001 * 18.5);
    }
    //根据身高返回建议体重的最大值 height是cm 返回的是kg
    public static float getIdeaWeightMax(int height){
        return (float) (height * height * 0.0001 * 24.0);
    }


    //kg转磅
    public static float kg2lb(float kg){
        return (float) (kg * 144480.0/65536.0);
    }
    //磅转kg
    public static float lb2kg(float lb){
        return (float) (lb * 65536.0/144480.0);
    }



    public static float adapterDataFloat(float value){
        if (!Constants.isKgUnit){
           return kg2lb(value);
        }
        return value;
    }


    //位置是否取反的适配方法
    public static String unitDisplay(String value,String unit){
        String local = AppUtils.getLocalStr();
        String result;
        if (TextUtils.equals(local,AppUtils.iw)){
            result =  value+unit;
            ULog.i(TAG,"unitDisplay result1 = "+result);
            //是要取反的
            return result;
        }else {
            //不需要取反的
            result = value+unit;
            ULog.i(TAG,"unitDisplay result2 = "+result);
            return result;
        }
    }


    //位置是否取反的适配方法
    public static String unitDisplay2(String value,String unit){
        String local = AppUtils.getLocalStr();
        String result;
        if (TextUtils.equals(local,AppUtils.iw)){
            result =  unit+value;
            ULog.i(TAG,"unitDisplay result1 = "+result);
            //是要取反的
            return result;
        }else {
            //不需要取反的
            result = value+unit;
            ULog.i(TAG,"unitDisplay result2 = "+result);
            return result;
        }
    }

    public static String placeHolder2(String result,String str1,String str2){
        String local = AppUtils.getLocalStr();
        if (TextUtils.equals(local,AppUtils.iw)){
            //是要取反的
            return String.format(result,str1,str2);
        }else {
            //不需要取反的
            return String.format(result,str1,str2);
        }
    }

    public static String placeHolder4(String result,float value1,String str1,float value2,String str2){
        String local = AppUtils.getLocalStr();
        if (TextUtils.equals(local,AppUtils.iw)){
            //是要取反的
            //return String.format(result,min,"kg",max,"kg");
            return "";
        }else {
            //不需要取反的
            return String.format(result,value1,str1,value2,str2);
        }
    }


    public static String unitDisplay(float value,String unit){
        return unitDisplay(value+"",unit);
    }


    //2018-16
    public static String intNumFatmat7(String days) {
        if (TextUtils.isEmpty(days)||days.length()!=7){
            return "";
        }
        int num=NumUtils.string2Int(days.substring(5,7));
        return SpecialText.numberLocle_i(ApplicationHelper.mApplicationContecx,num);
    }

    public static String monthFatmat(String days) {
        if (TextUtils.isEmpty(days)||days.length()!=7){
            ULog.i(TAG,"格式化月出现错误数据--"+days);
            return "";
        }
        int num=NumUtils.string2Int(days.substring(5,7));
        if (num<=0){
            ULog.i(TAG,"格式化月出现错误数据--"+days);
            return "";

        }
        String[] monthTexts = ApplicationHelper.mApplicationContecx.getResources().getStringArray(R.array.months);
        if (num>12){
            num=12;
            ULog.i(TAG,"格式化月出现错误数据--"+days);
        }
        return monthTexts[num-1];
    }



    public static void updateGravityByLocal(EditText... editTexts){
        String language= AppUtils.getLocalStr();
        int gravity;
        if (TextUtils.equals(language,AppUtils.iw)){
            gravity=Gravity.RIGHT;
        }else {
            gravity=Gravity.LEFT;
        }

        for (EditText e:editTexts){
            e.setGravity(gravity);
        }
    }
}
